package com.easy.system.service.impl;

import com.easy.system.dao.BaseDao;
import com.easy.system.service.BaseService;
import com.easy.system.util.Pagination;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sunguangzhu
 * Date: 13-7-1
 * Time: 下午2:53
 * To change this template use File | Settings | File Templates.
 */

public class BaseServiceImpl<T> implements BaseService<T> {
    protected Class<T> clazz;
    @Resource
    protected BaseDao<T> baseDao;

    public void setClazz(Class<T> clazz) {
        this.clazz = clazz;
    }

    @Override
    public void save(T entity) {
        //To change body of implemented methods use File | Settings | File Templates.
        this.baseDao.save(entity);
    }

    @Override
    public T get(Class<T> clazz, Long id) {
        return this.baseDao.get(clazz, id);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void update(T entity) {
        this.baseDao.update(entity);
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void delete(Class<T> clazz, Long id) {
        T entity = this.baseDao.get(clazz, id);
        this.baseDao.delete(entity);
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public int delete(Class<T> clazz, String ids) {
        return this.baseDao.delete(clazz, ids);
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<T> find(String hql, Pagination pagination, Object... params) {
        return this.baseDao.find(hql, pagination, params);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<T> find(Pagination pagination) {
        StringBuilder hql = new StringBuilder(" from " + clazz.getSimpleName());
        return this.baseDao.find(hql.toString(), pagination);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Long getTotal(String hql, Object... params) {
        return this.baseDao.getTotal(hql, params);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Long getTotal() {
        StringBuilder hql = new StringBuilder(" select count(id) from ").append(clazz.getSimpleName());
        return this.baseDao.getTotal(hql.toString());  //To change body of implemented methods use File | Settings | File Templates.
    }

}
